<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="类型名称" prop="typeName">
        <el-select v-model="dataForm.typeName" placeholder="类型名称" style="width:100%"  @change="chooseType" filterable clearable allow-create>
          <el-option v-for="param in paramList" :key="param.typeName" :value="param.typeName" >{{ param.typeName }}</el-option>
        </el-select>    
      </el-form-item>
      <el-form-item label="类型编码" prop="typeCode">
        <el-input v-model="dataForm.typeCode" placeholder="类型编码"></el-input>
      </el-form-item>
      <el-form-item label="参数名称" prop="paramName">
        <el-input v-model="dataForm.paramName" placeholder="参数名称"></el-input>
      </el-form-item>
      <el-form-item label="参数编码" prop="paramCode">
        <el-input v-model="dataForm.paramCode" placeholder="参数编码"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          typeName: '',
          typeCode: '',
          paramName: '',
          paramCode: '',
          remark: ''
        },
        paramList: [],
        dataRule: {
          typeName: [
            { required: true, message: '类型名称不能为空', trigger: 'blur' }
          ],
          typeCode: [
            { required: true, message: '类型编码不能为空', trigger: 'blur' }
          ],
          paramName: [
            { required: true, message: '参数名称不能为空', trigger: 'blur' }
          ],
          paramCode: [
            { required: true, message: '参数编码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.queryParamTypes()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/admin-interfaces/param/info/${this.dataForm.id}`),
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.typeName = data.data.typeName
                this.dataForm.typeCode = data.data.typeCode
                this.dataForm.paramName = data.data.paramName
                this.dataForm.paramCode = data.data.paramCode
                this.dataForm.remark = data.data.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin-interfaces/param/saveOrUpdateParam`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'typeName': this.dataForm.typeName,
                'typeCode': this.dataForm.typeCode,
                'paramName': this.dataForm.paramName,
                'paramCode': this.dataForm.paramCode,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data.data === true && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message({
                  showClose: true,
                  message: data.message,
                  type: 'warning',
                  center: true
                })
              }
            })
          }
        })
      },
      // 参数类型
      queryParamTypes () {
        this.$http({
          url: this.$http.adornUrl('/admin-interfaces/param/queryTypes'),
          method: 'get'
        }).then(({data}) => {
          this.paramList = data.data
        })
      },
      // 选择类型
      chooseType (typeName) {
        const foundItem = this.paramList.find(item => item.typeName === typeName)
        this.dataForm.typeCode = foundItem.typeCode
      }
    }
  }
</script>
